<?php

namespace App\Http\Controllers\Home;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function register(){
        return view('home.create');
    }

    public function store(Request $request){
//        dd($request->all());
        $this->validate($request,[
            'mobile'=>'required',
            'password'=>'required|min:6'
        ],[
            'mobile.required'=>'手机号码不能为空',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码不能少于6位'
        ]);
        $data['mobile']=$request->mobile;
        $data['password']=bcrypt($request->password);
//        dd($data);
        User::create($data);
        return view('home.login')->with('success','注册成功');
    }

    public function loginPage(){
        return view('home.login');
    }
    public function login(Request $request){
//        dd(1);
        $this->validate($request,[
            'mobile'=>'required',
            'password'=>'required'
        ],[
            'mobile.required'=>'手机号码不能为空',
            'password.required'=>'密码不能为空'
        ]);
        $status=\Auth::guard()->attempt([
            'mobile'=>$request->mobile,
            'password'=>$request->password
        ]);
        if ($status){
            return redirect()->route('home.index')->with('success','登录成功');
        }else{
            return back()->with('error','请输入正确的手机号和密码');
        }
    }

    public function logout(){
        \Auth::logout();
        return view('home.login');
    }

}
